package com.csi.controller;

import com.csi.domain.Result;
import com.csi.utils.OssUtil;
import com.csi.utils.ReturnResults;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
public class FileOssController {

    @RequestMapping("/file/upload")
    public Object uploadFile(@RequestParam("file") List<MultipartFile> uploadFiles, @RequestParam("files") String filesName) throws IOException {
        List<String> filesHrefs = new ArrayList<>();
        // 遍历文件列表，逐个上传
        for (MultipartFile uploadFile : uploadFiles) {
            // 调用 OssUtil 上传文件
            filesHrefs.add(OssUtil.upload(uploadFile.getOriginalFilename(), uploadFile.getInputStream(), filesName));

        }
        if (filesHrefs.size() == uploadFiles.size()){
            return new Result<List<String>>("上传图片成功！", ReturnResults.ACCESS_SUCCESS,filesHrefs);
        }
        return new Result<>("上传图片失败！",ReturnResults.ACCESS_FAILED);
    }
}
